package com.dfbz.controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Map;

public class BaseController extends HttpServlet {

    protected String getUserId(HttpServletRequest req){
        //获取当前登录过的用户ID
        return req.getSession().getAttribute("myUserId").toString();
    }
    protected String getUserName(HttpServletRequest req){
        //获取当前登录过的用户名、
        Map map=(Map)req.getSession().getAttribute("userBaseInfo");
        return map.get("username").toString();

    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String uri = req.getRequestURI();
            String funName = uri.substring(uri.lastIndexOf("/") + 1);
            Class<? extends BaseController> aClass = this.getClass();
            //用反射机制代理执行 this 当前类的方法
            Method method = aClass.getDeclaredMethod(funName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(aClass.newInstance(),req,resp);
        }catch (Exception e){
            e.printStackTrace();
        }


    }
}